package com.barneyx.mshinfoplatform.entity.info;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.barneyx.mshinfoplatform.entity.CommonEntity;
import com.barneyx.mshinfoplatform.entity.enums.EInfoState;
import com.barneyx.mshinfoplatform.entity.enums.EPlatformType;
import com.barneyx.mshinfoplatform.entity.user.UserEntity;
import com.barneyx.mshinfoplatform.entity.user.UserPlatformEntity;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.TableCharset;
import com.gitee.sunchenbin.mybatis.actable.annotation.TableComment;
import com.gitee.sunchenbin.mybatis.actable.annotation.TableEngine;
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlCharsetConstant;
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlEngineConstant;
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "msh_info_history_list", autoResultMap = true)
@TableCharset(MySqlCharsetConstant.UTF8MB4)
@TableEngine(value = MySqlEngineConstant.MyISAM)
@TableComment("消息修改历史表")
@Slf4j
public class InfoHistoryEntity extends CommonEntity {
    @TableId(value = "info_history_id", type = IdType.ASSIGN_ID)
    @Column(type = MySqlTypeConstant.BIGINT)
    private String InfoHistoryId;

    @TableField(value = "info_id")
    @Column(type = MySqlTypeConstant.BIGINT)
    private String InfoId;

    @ApiModelProperty("标题")
    @TableField("`title`")
    private String Title;

    @ApiModelProperty("用户编号")
    @TableField("user_id")
    @Column(isNull = false, type = MySqlTypeConstant.BIGINT)
    @NotNull(message = "所属用户UserId不能为空")
    private String UserId;

    @ApiModelProperty("所属用户")
    @TableField(exist = false)
    private UserEntity User;

    @ApiModelProperty("所属用户平台编号")
    @TableField("user_platform_id")
    @Column(isNull = false, type = MySqlTypeConstant.BIGINT)
    @NotNull(message = "所属平台不能为空!")
    private String UserPlatformId;

    @TableField(exist = false)
    private UserPlatformEntity UserPlatform;



    @ApiModelProperty("信息描述")
    @TableField("info_description")
    private String InfoDescription;

    @ApiModelProperty("信息图册")
    @TableField(value = "albums", typeHandler = FastjsonTypeHandler.class)
    @Column(type = MySqlTypeConstant.JSON)
    private JSONArray Albums;

    @ApiModelProperty("信息说明")
    @TableField(value = "remark")
    @Column(type = MySqlTypeConstant.LONGTEXT)
    private String Remark;

    @ApiModelProperty("信息封面")
    @TableField("conver")
    private String Conver;

    @ApiModelProperty("信息视频")
    @TableField("video")
    private String Video;

    @ApiModelProperty("信息分类")
    @TableField("info_cate_id")
    @Column(type = MySqlTypeConstant.BIGINT)
    private String InfoCateId;


    @TableField("cate_name")
    @Column(type = MySqlTypeConstant.CHAR, length = 20)
    private String CateName;


    @TableField("info_post_name")
    private String InfoPostName;

    @TableField("info_post_via")
    private String InfoPostVia;

    @ApiModelProperty("联系人")
    @TableField("ltd_contact")
    private String LtdContact;

    @ApiModelProperty("联系人电话")
    @TableField("ltd_phone")
    private String LtdPhone;


    @TableField("post_platform")
    @Column(comment = "信息是哪一个平台发布的", type = MySqlTypeConstant.TINYINT)
    @JsonDeserialize()
    private EPlatformType PlatformType;

    @ApiModelProperty("联系人微信")
    @TableField("ltd_wechat")
    private String LtdWechat;


    @TableField(value = "tags", typeHandler = JacksonTypeHandler.class)
    @Column(type = MySqlTypeConstant.JSON)
    private List<InfoCateTagsEntity> InfoTags = new ArrayList<>();


    public Object getImgs() {
        try {
            return ListUtil.sub(getAlbums(), 0, 4);
        } catch (Exception e) {
        }
        return null;
    }

    public boolean getIsAlbums() {
        return getAlbums().size() > 0;
    }

    public String getDesc(){
        return StrUtil.subPreGbk(getRemark(),120,"...");
    }
}
